<?php

namespace App\Http\Controllers\api;

use App\Constant\Enum;
use App\Models\Admodel;
use App\Models\Setting;
use Illuminate\Http\Request;

class AdmodelController extends BaseController
{
    public function configs(Request $request)
    {
        // @todo: 获取广告媒体ID
        $appId = Setting::get('media_id');

        $list = Admodel::query()
            ->with(['adtype'])
            ->whereHas('adtype', function ($query) {
                $query->where('status', Enum::STATUS_ENABLED);
            })
            ->where('status', Enum::STATUS_ENABLED)
            ->get();

        $result = [];
        foreach ($list as &$item) {
            if (empty($item->adtype)) {
                continue;
            }
            if (empty($item->override_reward_rate) && $item->override_reward_rate > 0) {
                $item->reward_rate = $item->override_reward_rate;
            } else {
                $item->reward_rate = $item->adtype->reward_rate;
            }
            $item->position = $item->adtype->position;
            $result[] = $item;
        }
        $data = [
            'media_id' => $appId,
            'list' => $result,
        ];

        return $this->success($data);
    }
}
